﻿using System.Security.Cryptography;
using System.Text;
using Wechat_PublicNumber.Entity;
using Wechat_PublicNumber.Model;
using Wechat_PublicNumber.SettingModel;

namespace Wechat_PublicNumber.Repository
{
    public class WxRepository : DataAccess
    {
        [Autowired]
        private WxSetting _wxSetting;

        [Autowired]
        private WxTemplateRepository _wxTemplateRepository;

        /// <summary>
        /// 微信接入校验绑定
        /// </summary>
        /// <returns></returns>
        public bool CheckWxBind(WeChatSignModel model)
        {
            var list = new List<string>
            {
                _wxSetting.Token,
                model.Timestamp,
                model.Nonce
            };
            list.Sort();

            byte[] array = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(string.Join("", list.ToArray())));
            StringBuilder stringBuilder = new StringBuilder();
            byte[] array2 = array;
            foreach (byte b in array2)
            {
                stringBuilder.AppendFormat("{0:x2}", b);
            }

            return model.Signature == stringBuilder.ToString();
        }
    }
}
